package com.mes.cloud.rabbit.producter.impl;

import com.mes.cloud.commons.Constants;
import com.mes.cloud.rabbit.bean.Message;
import com.mes.cloud.rabbit.producter.MessageProducter;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * MessageProducter
 *
 * @Author:WenJun Fan
 * @Date: 2018/6/14
 */
@Component
public class MessageProducterImpl implements MessageProducter{

    @Autowired
    private AmqpTemplate rabbitTemplate;

    private AmqpTemplate getAmqpTemplate(){
        ConnectionFactory factory = new CachingConnectionFactory("127.0.0.1",5672);
        if(rabbitTemplate == null){
            return new RabbitTemplate(factory);
        }
        return rabbitTemplate;
    }

    @Override
    public void sendMessage(String queues,Message message) {
        System.out.println("user send : " + message);
        AmqpTemplate amqpTemplate = getAmqpTemplate();
        amqpTemplate.convertAndSend(Constants.CCS_QUEUES, message);
    }


}
